package newBee.class4;

import cn.hutool.core.util.ArrayUtil;
import common.ArrayUtils;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 贪心算法
 * 123+456
 * 相当于 123 * k^3 + 456：123左移 456的位数
 * <p>
 * 场景1：[....a, m1,m2,b....] <= [....m1,a,m2, b....] a.m1 <= m1.a
 * 场景2：
 * *    [....a, m1,m2,b....]    a.m1 <= m1.a
 * *    [....m1,a ,m2,b....]    a.m2 <= m2.a
 * *    [....m1,m2,a ,b....]    a.b  <= b.a
 * *    [....m1,m2,b, a....]
 * *    [....b,m1,m2, a....]
 */
public class TestDemo {
    public static void main(String[] args) {
        // 字符串的最小组合
        int[] arr = {3, 30, 34, 5, 9};
        String ans = largestNumber(arr);
        System.out.println("ans = " + ans);
    }

    public static String largestNumber(int[] nums) {
        if (nums == null || nums.length == 0) {
            return "";
        }
        String[] arr = new String[nums.length];
        int i = 0;
        for (int num : nums) {
            arr[i++] = num + "";
        }
        Arrays.sort(arr, (a, b) -> (b + a).compareTo(a + b));
        if(arr[0].equals("0")){
            return "0";
        }
        StringBuilder b = new StringBuilder();
        for (String s : arr) {
            b.append(s);
        }

        return b.toString();
    }
}
